﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DnPakExplorer.Forms {
	public partial class EquipementAnalysis : Form, IEnumerable<IEquipmentProperyable> {
		public EquipementAnalysis() {
			InitializeComponent();
			EquipmentPanel ep = DnPlayerUtil.CurrentCharacter.EquipmentPanel;
			foreach (IEquipmentProperyable equip in ep.ItemSlots) {
				ListViewGroup group = listView1.Groups[0];
				if (equip.Id >= 0 && equip.Id<=10) {
					group = listView1.Groups[0];
				} else if (equip.Id >= 12 && equip.Id<=29) {
					group = listView1.Groups[1];
				} else if (equip.Id >= 30 && equip.Id<=41 || equip.Id >= 57 && equip.Id<=64) {
					group = listView1.Groups[2];
				} else if (equip.Id >= 42 && equip.Id<=56) {
					group = listView1.Groups[1];
				}
				ListViewItem item = group.Items.Add(equip.SlotName);
				item.Tag = equip.Id;
				item.SubItems.Add("");
				if (equip is ItemSlot) {
					if (((ItemSlot)equip).Item != null)
						item.SubItems[0].Text = ((ItemSlot)equip).Item.name;
				} else if (equip is Appellation) {
					item.SubItems[0].Text = ((Appellation)equip).Name;
				} else if (equip is HerolProperty) {
					item.SubItems[0].Text = "等级："+((HerolProperty)equip).Level.ToString();
				}
			}

		}

		private void listView1_ItemChecked(object sender, ItemCheckedEventArgs e) {

		}

		private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e) {

		}

		private IEnumerator<Property> AllSetsRroperties() {
			foreach (KeyValuePair<int, SetEntry> entry in mEquipedSets) {
				foreach (Property prop in entry.Value.properties) {
					if ((int)prop.tag <= GetSetItemCount(entry.Key)) {
						Property p = new Property();
						p.id = (PropertyID)((int)prop.id);
						p.value = prop.value;
						yield return p;
					}
				}
			}
		}

		private IEnumerator<IEquipmentProperyable> AllSelectedItems() {
			foreach (ListViewGroup group in listView1.Groups) {
				foreach (ListViewItem item in group.Items) {
					if (item.Checked) {
						yield return item.Tag as IEquipmentProperyable;
					}
				}
			}
		}

		public int GetSetItemCount(int setId) {
			if (mSetCounts.ContainsKey(setId))
				return mSetCounts[setId];
			return 0;
		}

		public Dictionary<int, int> mSetCounts = new Dictionary<int, int>();
		float[] mFloatProperty = new float[(int)PropertyID.ID_MAX];
		public Dictionary<int, SetEntry> mEquipedSets = new Dictionary<int, SetEntry>();

		void RecalculateProperty() {
			for (int i=0; i<mFloatProperty.Length; i++)
				mFloatProperty[i] = 0;
			mSetCounts.Clear();
			mEquipedSets.Clear();
			foreach (IEquipmentProperyable equip in this) {
				IItemSlot slot = equip as IItemSlot;
				if (slot != null && slot.Item != null && slot.Item.setId>0) {
					mEquipedSets[slot.Item.setId] = slot.Item.set;
					if (!mSetCounts.ContainsKey(slot.Item.setId)) {
						mSetCounts[slot.Item.setId] = 0;
					}
					mSetCounts[slot.Item.setId]++;
				}
			}

			int p, p1, p2;
			foreach (IEquipmentProperyable slot in this) {
				foreach (Property prop in slot) {
					p = PropertyUtil.GetPropertyOneMapTwo(((int)prop.id), out p1, out p2);
					if (PropertyUtil.IsPercentProperty((int)prop.id)) {
						if (p1>=0) {
							mFloatProperty[p1] += prop.value;
							mFloatProperty[p2] += prop.value;
						} else
							mFloatProperty[(int)prop.id] += prop.value;
					} else {
						if (p1>=0) {
							mFloatProperty[p1] += prop.value;
							mFloatProperty[p2] += prop.value;
						} else
							mFloatProperty[(int)prop.id] += prop.value;
					}
				}
			}

			IEnumerator<Property> setProps = AllSetsRroperties();
			while (setProps.MoveNext()) {
				Property prop = setProps.Current;
				p = PropertyUtil.GetPropertyOneMapTwo(((int)prop.id), out p1, out p2);
				if (PropertyUtil.IsPercentProperty((int)prop.id)) {
					if (p1>=0) {
						mFloatProperty[p1] += prop.value;
						mFloatProperty[p2] += prop.value;
					} else
						mFloatProperty[(int)prop.id] += prop.value;
				} else {
					if (p1>=0) {
						mFloatProperty[p1] += prop.value;
						mFloatProperty[p2] += prop.value;
					} else
						mFloatProperty[(int)prop.id] += prop.value;
				}
			}

			StringBuilder sb = new StringBuilder();
			sb.AppendFormat(@"[属性]\n");
			for (int i=0; i<mFloatProperty.Length; i++) {
				float value = mFloatProperty[i];
				if (value == 0)
					continue;
				if (PropertyUtil.IsPercentProperty(i))
					sb.AppendFormat(@"{0}:{1}%<br>", PropertyUtil.GetPropertyName(i), value*100);
				else
					sb.AppendFormat(@"{0}:{1}<br>", PropertyUtil.GetPropertyName(i), value);
			}
		}

		public IEnumerator<IEquipmentProperyable> GetEnumerator() {
			return AllSelectedItems();
		}

		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
			return GetEnumerator();
		}
	}
}
